package com.ishansong.ticket.business.service.impl;

import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.ishansong.ticket.business.service.TicketBusinessService;
import com.ishansong.ticket.enums.TicketStatusEnum;
import com.ishansong.ticket.exception.ServiceDaoException;
import com.ishansong.ticket.exception.ServiceException;
import com.ishansong.ticket.model.*;
import com.ishansong.ticket.service.*;
import com.ishansong.ticket.utils.Beans;
import com.ishansong.ticket.utils.DateUtil;
import com.ishansong.ticket.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by saiyin on 15/9/25.
 *
 */
@Service(value= "ticketBusinessService")
public class TicketBusinessServiceImpl implements TicketBusinessService {
    @Autowired
    private TicketService ticketService;
    @Autowired
    private TicketRecordService ticketRecordService;
    @Autowired
    private TicketQaGroupService ticketQaGroupService;
    @Autowired
    private TicketUserService ticketUserService;
    @Autowired
    private BindingUserService bindingUserService;
    @Override
    public long save(Ticket ticket) throws ServiceDaoException, ServiceException {
        try {
            ticket.setCreateTime(new Date());
            ticket.setUpdateTime(new Date());
            long ticketId = ticketService.saveTicket(ticket);
            TicketRecord ticketRecord = new TicketRecord();
            ticketRecord.setRemark(ticket.getRemark());
            ticketRecord.setStatus(ticket.getStatus());
            ticketRecord.setCreateTime(new Date());
            ticketRecord.setOperatorId(ticket.getOperatorId());
            ticketRecord.setTicketId(ticket.getId());
            ticketRecordService.saveTicketRecord(ticketRecord);
            return ticketId;
         } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public long update(Ticket ticket) throws ServiceDaoException, ServiceException {
        try {
            ticket.setUpdateTime(new Date());
            Ticket ticketUp = ticketService.getTicket(ticket.getId());
            if(ticketUp != null) {
                ticketUp.setQaQroupId1(ticket.getQaQroupId1());
                ticketUp.setQaQroupId2(ticket.getQaQroupId2());
                ticketUp.setQaQroupId3(ticket.getQaQroupId3());
                ticketUp.setRemark(ticket.getRemark());
                ticketService.updateTicket(ticketUp);
            }
            return ticket.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void updateTicketStatusById(long id, int status, String operatorId, String remark) throws ServiceDaoException, ServiceException {
        try {
            Ticket ticket = ticketService.getTicket(id);
            if(ticket.getStatus() == status) {
                throw new ServiceException("不能做重复操作");
            }
            ticket.setUpdateTime(new Date());
            ticket.setOperatorId(operatorId);
            if(status != -1) {
                ticket.setStatus(status);
            }
            if(StringUtils.isNotBlank(remark) && status != -1) {
                ticket.setRemark(remark);
            }
            ticketService.updateTicket(ticket);
            TicketRecord ticketRecord = new TicketRecord();
            ticketRecord.setCreateTime(new Date());
            ticketRecord.setRemark(remark);
            ticketRecord.setTicketId(id);
            ticketRecord.setOperatorId(operatorId);
            if(status != -1) {
                ticketRecord.setStatus(status);
            } else {
                ticketRecord.setStatus(ticket.getStatus());
            }
            ticketRecordService.saveTicketRecord(ticketRecord);
        } catch (ServiceDaoException e) {
            e.printStackTrace();
        }
    }

    @Override
    public TicketDataVo queryTicketById(long id) throws ServiceDaoException, ServiceException {
        Ticket ticket = ticketService.getTicket(id);
        List<TicketRecord> ticketRecords = ticketRecordService.queryTicketRecordsByTicketId(id);
        TicketDataVo vo = new TicketDataVo();
        Beans.copy(ticket, vo);
        List<TicketRecordVo> listVo = Lists.newArrayList();
        for(TicketRecord ticketRecord : ticketRecords) {
            TicketRecordVo ticketRecordVo = new TicketRecordVo();
            ticketRecordVo.setStatusName(TicketStatusEnum.getName(ticketRecord.getStatus()));
            ticketRecordVo.setCreateTime(ticketRecord.getCreateTime());
            ticketRecordVo.setRemark(ticketRecord.getRemark());
            ticketRecordVo.setTicketId(ticketRecord.getTicketId());
            ticketRecordVo.setOperatorId(ticketRecord.getOperatorId());
            listVo.add(ticketRecordVo);
        }
        vo.setList(listVo);
        return vo;
    }

    @Override
    public List<TicketQaGroup> queryTicketQaGroupsByPid(Integer pid,Integer type) throws ServiceDaoException, ServiceException {
        return ticketQaGroupService.queryTicketQaGroupsByPid(pid,type);
    }

    @Override
    public int queryTicketCountByOrderNumber(String orderNumber) throws ServiceDaoException, ServiceException {
        return ticketService.queryTicketCountByOrderNumber(orderNumber);
    }

    @Override
    public List<TicketListVo> queryTicketByOrderNumber(String orderNumber) throws ServiceDaoException, ServiceException {
        List<Ticket> list = ticketService.queryTicketByOrderNumber(orderNumber);
        List<TicketListVo> listVos = Lists.newArrayList();
        for(Ticket ticket : list) {
            TicketListVo vo = packageListVo(ticket);
            listVos.add(vo);
        }
        return listVos;
    }

    @Override
    public Long saveTicketUser(TicketUser ticketUser) throws ServiceDaoException, ServiceException {
        return ticketUserService.saveTicketUser(ticketUser);
    }

    @Override
    public PageVo<TicketListVo> queryTicketByPage(Integer status,int start,int pageSize,String operatorId) throws ServiceDaoException, ServiceException {
        int count = ticketService.queryTicketCountByStatus(status,operatorId);
        List<Ticket> list = ticketService.queryTicketPageByStatus(status, start, pageSize,operatorId);
        List<TicketListVo> listVos = Lists.newArrayList();
        for(Ticket ticket : list) {
            TicketListVo vo = packageListVo(ticket);
            listVos.add(vo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setCount(count);
        pageVo.setList(listVos);
        pageVo.setPageSize(pageSize);
        pageVo.setStart(start);
        return pageVo;
    }

    @Override
    public List<TicketListVo> queryMyTicketByStatus(Integer status,String operatorId) throws ServiceDaoException, ServiceException {
        List<TicketListVo> listVos = Lists.newArrayList();
        if(status != TicketStatusEnum.BC.getStatus()) {
            List<TicketRecord> listTicketRecord = ticketRecordService.queryTicketRecordsByOperatorIdAndStatus(operatorId, status);
            for(TicketRecord ticketRecord : listTicketRecord) {
                Ticket ticket = ticketService.getTicket(ticketRecord.getTicketId());
                if(ticket != null) {
                    TicketListVo vo = packageListVo(ticket);
                    listVos.add(vo);
                }
            }
        } else {
            // 保存状态是临时状态，改变状态后不会在列表中显示
            List<Ticket> tickets = ticketService.queryTicketPageByStatus(status,0,100,operatorId);
            for(Ticket ticket : tickets) {
                TicketListVo vo = packageListVo(ticket);
                listVos.add(vo);
            }
        }
        return listVos;
    }

    @Override
    public String helloWord() {
        return "调用成功";
    }

    @Override
    public List<TicketListVo> queryTicketByMobile(String mobile) throws ServiceDaoException, ServiceException {
        List<Ticket> list = ticketService.queryTicketByMobile(mobile);
        List<TicketListVo> listVos = Lists.newArrayList();
        for(Ticket ticket : list) {
            TicketListVo vo = packageListVo(ticket);
            listVos.add(vo);
        }
        return listVos;
    }

    @Override
    public void updateTicketStarus(Long[] ids, String operatorId) throws ServiceDaoException, ServiceException {
        for(long id : ids) {
            Ticket ticket = ticketService.getTicket(id);
            if(ticket.getStatus() == TicketStatusEnum.CLZ.getStatus()) {
                continue;
            }
            ticket.setStatus(TicketStatusEnum.CLZ.getStatus());
            ticket.setOperatorId(operatorId);
            ticketService.updateTicket(ticket);
        }

    }

    @Override
    public void deleteTicketById(long id, String operatorId) throws ServiceDaoException, ServiceException {
        Ticket ticket = ticketService.getTicket(id);
        if(ticket != null) {
            if(ticket.getStatus() ==TicketStatusEnum.BC.getStatus()) {
                if(operatorId.equals(ticket.getOperatorId())) {
                    ticketService.deleteTicket(id);
                    List<TicketRecord> list = ticketRecordService.queryTicketRecordsByTicketId(id);
                    for(TicketRecord ticketRecord : list) {
                        ticketRecordService.deleteTicketRecord(ticketRecord.getId());
                    }
                }
            }
        }
    }

    @Override
    public BindingUserVo queryBindingUserByLoginName(String loginName) throws ServiceDaoException, ServiceException {
        BindingUser bindingUser =  bindingUserService.queryBindingUserByLoginName(loginName);
        BindingUserVo vo = new BindingUserVo();
        Beans.copy(bindingUser, vo);
        return vo;
    }

    @Override
    public TicketUser queryTicketUserByMobile(String mobile) throws ServiceDaoException, ServiceException {
        return ticketUserService.queryTicketUserByMobile(mobile);
    }

    @Override
    public Map<Integer, Integer> queryTicketCountByStatus(String operatorId) throws ServiceDaoException, ServiceException {
        int count1 = ticketService.queryTicketCountByStatus(1,operatorId);
        int count2 = ticketService.queryTicketCountByStatus(2,operatorId);
        int count3 = ticketService.queryTicketCountByStatus(3,operatorId);
        Map<Integer,Integer> map = Maps.newHashMap();
        map.put(1,count1);
        map.put(2,count2);
        map.put(3,count3);
        return map;
    }

    @Override
    public Map<Integer, Integer> queryTicketMyCountByStatus(String operatorId) throws ServiceDaoException, ServiceException {
        int count1 = ticketService.queryTicketCountByStatus(0,operatorId);
        int count2 = ticketRecordService.queryTicketMyCountByStatus(1, operatorId, DateUtil.getTime());
        int count3 = ticketRecordService.queryTicketMyCountByStatus(3,operatorId,DateUtil.getTime());
        Map<Integer,Integer> map = Maps.newHashMap();
        map.put(1,count1);
        map.put(2,count2);
        map.put(3,count3);
        return map;
    }

    /**
     * 封装接口数据
     * @param ticket 工单
     * @return TicketListVo
     * @throws ServiceDaoException
     * @throws ServiceException
     */
    private TicketListVo packageListVo(Ticket ticket) throws ServiceDaoException, ServiceException{
        Map<Integer,String> map = ticketQaGroupService.queryTicketQaGroups();
        TicketListVo vo = new TicketListVo();
        vo.setCreateTime(ticket.getCreateTime());
        vo.setId(ticket.getId());
        vo.setNumber(ticket.getNumber());
        vo.setQaQroupName1(map.get(ticket.getQaQroupId1()));
        vo.setQaQroupName2(map.get(ticket.getQaQroupId2()));
        vo.setQaQroupName3(map.get(ticket.getQaQroupId3()));
        vo.setStatus(TicketStatusEnum.getName(ticket.getStatus()));
        vo.setOrderNumber(ticket.getOrderNumber());
        return vo;
    }
}
